package com.pushmsg.wappush;

import java.util.*;

public class WapPushMsgHdr
{
	private byte  hdrlen;
	final private byte  udhie = 0x05;
	final private byte  portlen = 0x04;
	final private short destport =2948 ;
	final private short srcport = 9200;

	final private byte   msgsar=0x00;
	final private byte  udhsarlen = 0x03;
	private byte  refno ;

	private byte totno;
	private byte curno;


	private byte[] data = null;

	public WapPushMsgHdr()
	{
		byte[] b = new byte[1];
		Random random = new Random();
		random.nextBytes(b);
		refno = b[0];
	}

	public void setCurno(int no)
	{
		curno = (byte)no;
	}

	public void setTotalno(int no)
	{
		totno = (byte)no;
		if(no>1)
			hdrlen = 0x0B;
		else
			hdrlen = 0x06;
	}

	protected int getLength()
	{
		if(totno<=1)
			return 7;
		else
			return 12;
	}

	protected byte[] getData()
	{
		int ndx=0;
		if(totno>1)
			data = new byte[12];
		else
			data = new byte[7];
		data[ndx++] = hdrlen;
		data[ndx++] = udhie;
		data[ndx++] = portlen;
		data[ndx++] = (byte)((destport>>8)&0xFF);
		data[ndx++] = (byte)(0xFF&destport);
		data[ndx++] = (byte)((srcport>>8)&0xFF);
		data[ndx++] = (byte)(0xFF&srcport);

		if(totno>1)
		{
			data[ndx++] = msgsar;
			data[ndx++] = udhsarlen;
			data[ndx++] = refno;
			data[ndx++] = totno;
			data[ndx++] = curno;
		}
		return data;
	}
}